/**
 * Date:2019年9月24日 下午1:54:11
 * Copyright (c) 2019, www.bwbroad.com All Rights Reserved
 */

package com.app.common.core;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * Description: service基接口 <br/>
 * Date:2019年9月24日 下午1:54:11 <br/>
 * 
 * @author xuejianxin
 */
public interface IBaseService<T, PK extends Serializable> {

	IBaseDAO<T, PK> getDao();

	default T queryById(PK id) {
		return getDao().queryById(id);
	}

	default List<T> queryForList(Map<String, Object> params) {
		return getDao().queryAll(params);
	}

//	default PageInfo<T> queryByPage(Map<String, Object> params, boolean count) {
//		Integer pageNum = (Integer) params.get("pageNum");
//		Integer pageSize = (Integer) params.get("pageSize");
//		Assert.notNull(pageNum, "pageNum must be not null!");
//		Assert.notNull(pageSize, "pageSize must be not null!");
//		PageHelper.startPage(pageNum, pageSize, count);
//		return PageInfo.of(getDao().queryAll(params));
//	}
//
//	default PageInfo<T> queryByPage(Map<String, Object> params) {
//		return queryByPage(params, true);
//	}
	
	
	
	private <X> PageInfo<X> getPageParams(Map<String, Object> params) {
		Integer pageNum = (Integer) params.get("pageNum");
		Integer pageSize = (Integer) params.get("pageSize");
		Assert.notNull(pageNum, "pageNum must be not null!");
		Assert.notNull(pageSize, "pageSize must be not null!");
		PageInfo<X> pi = new PageInfo<>();
		pi.setPageNum(pageNum);
		pi.setPageSize(pageSize);
		return pi;
	}

	@SuppressWarnings("unchecked")
	default <X> PageInfo<X> queryByPage(Map<String, Object> params, boolean isCount) {
		return queryByPage(params, (ps) -> (List<X>) getDao().queryAll(ps), isCount);
	}

	default <X> PageInfo<X> queryByPage(Map<String, Object> params) {
		return queryByPage(params, true);
	}

	default <X> PageInfo<X> queryByPage(Map<String, Object> params,
			Function<Map<String, Object>, List<X>> queryCallback, boolean isCount) {
		PageInfo<X> pi = getPageParams(params);
		PageHelper.startPage(pi.getPageNum(), pi.getPageSize(), isCount);
		return PageInfo.of(queryCallback.apply(params));
		//List<X> list=queryCallback.apply(params);
		//pi.setList(list);
		//return pi;
	}
	
	

	// @Transactional(readOnly = true)
	default Optional<T> queryOne(Map<String, Object> params) {
		List<T> list = getDao().queryAll(params);
		if (null != list && list.size() > 1) {
			Assert.isTrue(list.size() <= 1, "queryOne 结果必须返回一行,现在返回" + list.size() + "行");
		}
		return Optional.ofNullable((null == list || list.isEmpty()) ? null : list.get(0));
	}

	// @Transactional(readOnly = true)
	default Long queryCount(Map<String, Object> params) {
		return getDao().queryCount(params);
	}

	default Integer save(T entity) {
		return getDao().save(entity);
	}

	//@Transactional(readOnly = false)
	default Integer update(T entity) {
		return getDao().update(entity);
	}

	default int deleteByIds(List<PK> ids) {
		return getDao().deleteByIds(ids);
	}

	default int deleteById(PK id) {
		return getDao().deleteByIds(Arrays.asList(id));
	}

}
